home *** CD-ROM | disk | FTP | other *** search
- Path: ccshst05.cs.uoguelph.ca!ccshst01!thay
- From: thay@uoguelph.ca (Toby K Hay)
- Newsgroups: comp.lang.c
- Subject: Re: Rand() Function
- Date: 21 Mar 1996 14:53:18 GMT
- Organization: University of Guelph
- Message-ID: <4irqgu$gml@ccshst05.cs.uoguelph.ca>
- References: <4iqltj$40g@news1.sympatico.ca>
- NNTP-Posting-Host: ccshst01.cs.uoguelph.ca
- X-Newsreader: TIN [version 1.2 PL2]
-
- Gisele Swinson (gisele.swinson@sympatico.ca) wrote:
- : How do I get a random number from a "SET" of numbers.
- : I want a random number from this set:
- : 2, 4, 6, 8, 10
- : It seems so simple but I can't find anything in my text how to do this.
-
- The FAQ for this group has the following for numbers chosen at random in
- the range 0 to N-1: rand()/(RAND_MAX/N + 1)
- For the above sequence you would use 5 for N, multiply by 2, and add 2.
- result = 2 + (2 * rand()/(RAND_MAX/5 + 1))
- Toby Hay thay@uoguelph.ca
-